package ict;

import ict.mobile.R;
import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.*;
import android.view.*;
import android.widget.*;

public class loginActivity extends Activity {
	SQLiteDatabase db;
	String sql;
	Cursor cursor = null;
	String[] columns = { "mid", "name", "password", "age" };
	String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name",
			"Password", "Age");
	String dataStr;
	EditText editLoginName, editPassword;
	Button buttonLogin, buttonClean;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginpage);

		findViewBy();

	}

	// ////find view by ID
	public void findViewBy() {
		editLoginName = (EditText) findViewById(R.id.editLoginName);
		editPassword = (EditText) findViewById(R.id.editPassword);
		buttonLogin = (Button) findViewById(R.id.buttonLogin);
		buttonClean = (Button) findViewById(R.id.buttonClean);

	}

	
	public void onClickToLogin(View view) {
		
		String output = editLoginName.getText().toString() + '\n' + editPassword.getText().toString();
		//Toast.makeText(this, output, 1).show();
		loginStaff();
		
		
	}
	
	public void onClickToClean(View view) {
		editLoginName.setText("");
	}

	// ////Display All of the database
	public void loginStaff(){
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			//cursor = db.rawQuery("select * from Staff where StaffID = 1101", null);
			//cursor = db.rawQuery("select * from Staff where LoginName = 'staff01'", null);
			cursor = db.rawQuery("select * from Staff where LoginName = " + "'"+ editLoginName.getText().toString() + "'", null);
			
			String dataStr = String.format("%4s %-10s %7s\n", "StaffID", "LoginName",
					"LoginPswd");
			
			int StaffID;
			String LoginName = null, LoginPswd = null;
			
			while (cursor.moveToNext()) {
				StaffID = cursor.getInt(cursor.getColumnIndex("StaffID"));
				// or cursor.getInt(0);
				LoginName = cursor.getString(cursor.getColumnIndex("LoginName"));
				// or cursor.getString(3);
				LoginPswd = cursor.getString(cursor
						.getColumnIndex("LoginPswd"));
				// or cursor.getString(4);
				//dataStr += String.format("%4d %-10s %4s\n", StaffID, LoginName, LoginPswd);
			}
			db.close();
			
			if (LoginName.equals(editLoginName.getText().toString()) && LoginPswd.equals(editPassword.getText().toString())){
				Toast.makeText(this, "Successfully", Toast.LENGTH_LONG).show();
				Intent result = new Intent();
				result.putExtra("loginName", editLoginName.getText().toString());
				result.putExtra("Password", editPassword.getText().toString());
				setResult(RESULT_OK, result);
				
				finish();
			}
			
			

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}
	
	///public 
	
	/*
	@Override
	public void finish() {
		Intent result = new Intent();
		result.putExtra("loginName", editLoginName.getText().toString());
		result.putExtra("Password", editPassword.getText().toString());
		setResult(RESULT_OK, result);
		super.finish();
	}	
	*/

}